<?php

/*
 * FCK Editor simple class by Federico Capoano
 * Created on 11 March 2009
 *
 * This file is NOT part of the symfony package.
 * This class is distributed under a GNU/GPL license
 *
 * Thanks for xpuc in the symfony forum for the suggestion to get the RelativeUrl
 */

/**
 * sfWidgetFormTextareaFCK represents a FCK Editor widget.
 *
 * You must include the FCK Editor JavaScript file by yourself in /web/js/editor/.
 *
 */
class sfWidgetFormTextareaFCK extends sfWidgetFormTextarea
{
  /**
   * Constructor.
   * toolbar: Default, Basic
   * skin: default, silver, office2003
   * width: specify the width of the editor, 650px as default
   * height: specify the height of the editor, 400px as default
   */
  protected function configure($options = array(), $attributes = array())
  {
    $this->addOption('toolbar', 'Default');
    $this->addOption('skin', 'default');
    $this->addOption('width', '650');
    $this->addOption('height', '400');
  }

  /**
   * @param  string $name        The element name
   * @param  string $value       The value selected in this widget
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {

    $request = sfContext::getInstance()->getRequest();
    $project_root = $request->getRelativeUrlRoot();
    $js = '/js/fckeditor/';

    require_once(sfConfig::get('sf_web_dir').$js.'fckeditor.php');
    $path = $project_root.$js;

    $fck = new FCKeditor($name);
    $fck->BasePath	= $path;
    $fck->Value		= $value;
    $fck->Width      = $this->getOption('width');
    $fck->Height      = $this->getOption('height');
    $fck->Config['SkinPath'] = $path . 'editor/skins/' . $this->getOption('skin') . '/';
    $fck->ToolbarSet = $this->getOption('toolbar');

    return $fck->Create();
  }
}
